<template>
  <div class="form-container">
    <el-form inline :model="form" ref="refForm" class="form-line" label-width="100px">
      <el-form-item label="商品名称" prop="goodsName">
        <el-input size="default" v-model="form.goodsName" placeholder="请输入商品名称"></el-input>
      </el-form-item>
      <el-form-item label="商品编号" prop="goodsId">
        <el-input size="default" v-model="form.goodsId" placeholder="请输入商品编号"></el-input>
      </el-form-item>
      <el-form-item label="所属供应商" prop="labelScope">
        <el-select size="default" v-model="form.supplierCode" placeholder="请选择范围">
          <el-option
            v-for="item in shopList"
            :key="item.merchantId"
            :label="item.shopName"
            :value="item.merchantId as string"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div class="btn-container">
      <el-button type="primary" @click="handleReset(refForm as FormInstance)">重置</el-button>
      <el-button type="primary" @click="getInventoryList">查询</el-button>
    </div>
  </div>
  <div class="table-container">
    <el-table
      border
      :cell-style="{ textAlign: 'center', fontSize: '13px' }"
      :header-cell-style="{
        textAlign: 'center',
        fontWeight: '100',
        fontSize: '13px',
        color: 'white',
        backgroundColor: '#009688'
      }"
      :data="inventoryList"
      style="width: 100%"
    >
      <el-table-column fixed prop="goodsId" label="商品编号">
        <template #default="scope">
          <span
            >{{ scope.row.goodsId
            }}<el-button link type="primary" size="small" v-copy="scope.row.goodsId" :icon="DocumentCopy"></el-button
          ></span>
        </template>
      </el-table-column>
      <el-table-column prop="goodsName" label="商品名称" />
      <el-table-column prop="supplierName" label="所属供应商" />
      <el-table-column prop="sortingInventory" label="待分拣库存结余" />
      <el-table-column prop="salesInventory" label="可销售库存结余" />
      <el-table-column fixed="right" label="操作">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="handleDetail(scope.row.goodsId)">查看详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
        background
        size="small"
        v-model:current-page="form.pageNum"
        :page-sizes="[5, 10, 15, 20]"
        layout="prev, pager, next"
        @change="getInventoryList"
        :total="pageTotal"
        :page-size="form.pageSize"
      />
    </div>
  </div>
</template>

<script setup lang="tsx" name="useProTable">
import { onMounted, ref } from "vue";
import { Inventory } from "@/api/interface/province/inventory";
import { ElMessage, FormInstance } from "element-plus";
import { getInventoryListAPI, getShopListAPI } from "@/api/modules/province/inventoryAPI";
import { DocumentCopy } from "@element-plus/icons-vue";
import { useRouter } from "vue-router";
const router = useRouter();
let pageTotal = ref<number>(0);
let form = ref<Inventory.SearchParams>({
  city: "",
  province: JSON.parse(localStorage.getItem("userInfo") as string).province,
  pageNum: 1,
  pageSize: 10
});
let inventoryList = ref<Inventory.Row[]>([]);
const refForm = ref<FormInstance>();
const handleReset = (formEl: FormInstance) => {
  formEl.resetFields();
};
const getInventoryList = async () => {
  const { rows, code, msg, total } = await getInventoryListAPI(form.value);
  if (code == "200" && rows && total) {
    inventoryList.value = rows;
    pageTotal.value = total;
  } else {
    ElMessage.error(msg);
  }
};
let shopList = ref<Inventory.ShopList[]>([]);
const getShopList = async () => {
  const { data, code, msg } = await getShopListAPI();
  if (code == "200" && data) {
    shopList.value = data;
  } else {
    ElMessage.error(msg);
  }
};
const handleDetail = (id: string) => {
  router.push({ path: "/provinceLevel/proInventory/detail", query: { goodsId: id } });
};
onMounted(() => {
  getInventoryList();
  getShopList();
});
</script>
<style lang="scss" scoped>
::v-deep .el-button > span {
  font-size: 13px;
}
::v-deep .el-form-item__label {
  font-size: 13px;
}
::v-deep .el-input__inner {
  font-size: 13px;
}
::v-deep .el-select_wapper {
  font-size: 13px;
}
::v-deep .el-select__placeholder.is-transpar {
  font-size: 13px;
}
.form-container {
  padding: 10px;
  margin-bottom: 10px;
  background-color: white;
  .btn-container {
    display: inline-block;
    float: right;
    vertical-align: middle;
  }
  .el-form {
    display: inline-block;
  }
  .form-line {
    .el-form-item {
      width: 260px;
      margin: 0;
      margin-right: 20px;
    }
    .el-input {
      width: 240px;
    }
    .el-select {
      width: 240px;
    }
  }
}
.table-container {
  padding: 40px 10px;
  padding-bottom: 60px;
  background-color: white;
  .pagination-container {
    // display: inline-block;
    float: right;
    margin-top: 20px;
    background-color: white;
  }
}
</style>
